<%@ page import="bean.Article" %>
<%@ page import="bean.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="bean.Comment" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="bean.Label" %><%--
  Created by IntelliJ IDEA.
  User: 蒋缇
  Date: 2020/10/4
  Time: 11:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    Article article=(Article)request.getSession().getAttribute("blogArticle");
    ArrayList<Comment> comments=(ArrayList<Comment>)request.getSession().getAttribute("blogComments");
    ArrayList<Article> articleCollection=(ArrayList<Article>) request.getSession().getAttribute("userCollections");
    ArrayList<Article> articleLike=(ArrayList<Article>)request.getSession().getAttribute("userLikes");
    User user=(User)request.getSession().getAttribute("user");
    article.setCommentCount(article.getComments().size());
%>
<html>
<head>
    <title>查看博客</title>
<%--    <link rel="shortcut icon" href="images/magic3.jpg"/>--%>
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="editor/lib/marked.min.js"></script>
    <script src="editor/lib/prettify.min.js"></script>
    <script src="editor/lib/raphael.min.js"></script>
    <script src="editor/lib/underscore.min.js"></script>
    <script src="editor/lib/sequence-diagram.min.js"></script>
    <script src="editor/lib/flowchart.min.js"></script>
    <script src="editor/lib/jquery.flowchart.min.js"></script>
    <script src="editor/editormd.js"></script>
    <link rel="stylesheet"  href="css/bootstrap.min.css"/>
    <script src="js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="css/allStyle.css">
    <script type="text/javascript">
        let testEditor;
        $(function() {
            testEditor = editormd.markdownToHTML("doc-content", {//注意：这里是上面DIV的id
                htmlDecode : "style,script,iframe",
                emoji : true,
                taskList : true,
                tex : true, // 默认不解析
                flowChart : true, // 默认不解析
                sequenceDiagram : true, // 默认不解析
                codeFold : true
            });
        });
    </script>
</head>
<body>
<%--    导航条--%>
<div class="row">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <%--                    TBLOG标签--%>
                <a class="navbar-brand" href="#">TBLOG</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <%--                    主页链接--%>
                <ul class="nav navbar-nav">
                    <li><a href="websiteHomepage.jsp">首页🏠</a></li>
                    <%--                        下拉菜单--%>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">其他 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>
                </ul>
                <%--                     搜索栏--%>
                <form class="navbar-form navbar-left">
                    <input type="text" id="searchContent" class="form-control" size="125" maxlength="200" placeholder="请输入您想查询的内容">
                    <button type="button" class="btn btn-default" onclick="search()">🔍</button>
                </form>
                <%--                    链接--%>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="writeBlogPage.jsp">创作中心</a></li>
                    <%--                        下拉菜单--%>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><img src="<%=user.getHeadPortrait().equals("")?"images/defaultHeadPortrait.jpg":user.getHeadPortrait()%>" alt="无法加载" class="img-circle" width="20px" height="20px"><span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="personalHompage.jsp">个人中心</a></li>
                            <li><a href="loginPage.jsp">账号设置</a></li>
                            <li><a href="forgetPage.jsp">找回密码</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="successPage.jsp">退出</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
</div>
<div class="row">
        <div class="col-lg-3"></div>
        <div class="col-lg-6">
            <div class="panel panel-default" id="blogContent">
                <div class="panel-body">
    <%--                   标题--%>
                    <div class="row">
                        <div class="col-lg-4"></div>
                        <div class="col-lg-4">
                            <div class="panel-title" ><h2 class=""><%=article.getTitle()%></h2></div>
                        </div>
                        <div class="col-lg-4"></div>
                    </div>
<%--                     博客属性--%>
                    <div class="row">
                        <span><%=article.getTime()%> </span>
                        <span>作者：<%=article.getUser()%> </span>
                        <span>分栏专栏：<%=article.getSubfield()%></span>
                        <span>类型：<%=article.getType()%></span>
                    </div>
    <%--                    标签--%>
                    <div class="row">
                        <div class="panel panel-default" id="myLabel">
                            <div class="panel-body">
                                <%
                                    for(Label label:article.getLabel()){
                                %>
                                <span class="label label-default"><%=label.getContent()%>&nbsp</span>
                                <%
                                    }
                                %>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <div class="center-block">
                                    <!-- 用于显示md编辑器的md格式 -->
                                    <div id="doc-content">
                                        <textarea style="display: block"><%=article.getContent()%></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-6">
                            <b>浏览数：</b><span><%=article.getViews()%></span>
                            <b>喜爱度：</b><span><%=article.getLikingDegree()%></span>
                        </div>
                        <div class="col-lg-1">
                            <div data-toggle="tooltip" data-placement="top" title="<%=article.getCommentCount()%>">
                                <button class="btn btn-primary btn-circle btn-xs" type="button" data-toggle="collapse" data-target="#comment" aria-expanded="false" aria-controls="collapseExample">评论
                                </button>
                            </div>
                        </div>
                        <div class="col-lg-4">
                            <%
                                if(article.getUser().equals(user.getAccount())){
                            %>
                            <button class="btn btn-info btn-circle btn-xs" onclick="editBlog()">编辑</button>
                            <button class="btn btn-danger btn-circle btn-xs" onclick="deleteBlog()">删除</button>
                            <%
                                }else{
                            %>
                            <%
                                boolean flag1=false;/*查看收藏列表*/
                                boolean flag2=false;/*查看点赞列表*/
                                for(Article article1:articleCollection){
                                    if(article.getId()==article1.getId()){
                                        flag1=true;
                                        break;
                                    }
                                }
                                for(Article article1:articleLike){
                                    if(article.getId()==article1.getId()){
                                        flag2=true;
                                        break;
                                    }
                                }
                            %>
                            <%
                                if(flag1){
                            %>
                            <button class="btn btn-warning btn-circle btn-xs" onclick="cancelCollected()">取消收藏</button>
                            <%
                                }else{
                            %>
                            <button class="btn btn-warning btn-circle btn-xs" onclick="collectBlog()">收藏</button>
                            <%
                                }
                            %>
                            <%
                                if(flag2){
                            %>
                            <button class="btn btn-danger btn-circle btn-xs" onclick="cancelLiked()">取消点赞</button>
                            <%
                                }else{
                            %>
                            <button class="btn btn-danger btn-circle btn-xs" onclick="likeBlog()">点赞</button>
                            <%
                                }
                            %>
                            <%
                                }
                            %>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-3"></div>
    </div>
    <div class="row">
        <div class="col-lg-3"></div>
        <div class="col-lg-6">
            <div class="collapse" id="comment">
                <div class="well">
                    <%--评论输入框--%>
                    <div class="row">
                        <form action="AddCommentServlet" method="post">
                            <div class="row">
                                <div class="col-md-1"></div>
                                <div class="col-lg-9">
                                    <input class="form-control"  name="addArticleCommentContent" placeholder="回复<%=article.getUser()%>">
                                    <input style="display: none" name="addArticleCommentFatherId" value="<%=article.getId()%>">
                                    <input style="display: none" name="addArticleCommentId" value="<%=article.getId()%>">
                                </div>
                                <div class="col-lg-1"><button type="submit" class="btn btn-primary">评论</button></div>
                            </div>
                        </form>
                    </div>
                    <div class="row">
                        <ul id="commentList">
                            <%
                                for(int i=0;i<article.getComments().size();i++){
                                    if(comments.get(i).getFatherId()==article.getId()) {

                            %>
                            <li class="list-group-item">
                                <%--父评论--%>
                                <div class="row">
                                    <img src="<%=comments.get(i).getHeadPortrait().equals("")?"images/defaultHeadPortrait.jpg":comments.get(i).getHeadPortrait()%>" alt="无法加载" class="img-circle" width="40px" height="40px">
                                    <span><%=comments.get(i).getCommenter()%>:</span><span><%=comments.get(i).getContent()%></span>
                                    <font style="font-size: 10px"><%=comments.get(i).getTime()%></font>
                                    <button class="btn btn-primary btn-xs"  type="button" data-toggle="collapse" data-target="#<%=comments.get(i).getId()%>" aria-expanded="false" aria-controls="collapseExample">
                                        回复
                                    </button>
                                    <div class="collapse" id="<%=comments.get(i).getId()%>">
                                        <div class="well">
                                            <div class="row">
                                                <form action="AddCommentServlet" method="post">
                                                    <div class="col-lg-10">
                                                        <input class="form-control"  name="addArticleCommentContent" placeholder="回复<%=comments.get(i).getCommenter()%>">
                                                        <input style="display: none" name="addArticleCommentFatherId" value="<%=comments.get(i).getId()%>">
                                                        <input style="display: none" name="addArticleCommentId" value="<%=article.getId()%>">
                                                    </div>
                                                    <div class="col-lg-2"><button type="submit" class="btn btn-primary">评论</button></div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <%
                                    for (int j = i + 1; j < article.getComments().size(); j++) {
                                        if(comments.get(j).getFatherId()==comments.get(i).getId()){
                                %>
                                <%--子评论--%>
                                <div class="row">
                                    <div class="col-lg-1"></div>
                                    <div class="col-lg-11">
                                        <img src="<%=comments.get(j).getHeadPortrait().equals("")?"images/defaultHeadPortrait.jpg":comments.get(j).getHeadPortrait()%>" alt="无法加载" class="img-circle" width="40px" height="40px">
                                        <span><%=comments.get(j).getCommenter()%>:</span><span><%=comments.get(j).getContent()%></span>
                                        <font style="font-size: 10px"><%=comments.get(j).getTime()%></font><br>
                                    </div>
                                </div>
                                <%
                                                }
                                            }
                                        }
                                    }
                                %>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-3"></div>
    </div>
    <script type="text/javascript">
        window.onunload =  function (){
            <%
            article.setViews(article.getViews()+1);
            %>
            $.ajax(
                {
                    data: {"account":"<%=article.getId()%>","views": "<%=article.getViews()%>", "likingDegree":"<%=article.getLikingDegree()%>","commentCount":"<%=article.getCommentCount()%>"},
                    dataType: 'text',
                    type: 'post',
                    url: '<%=basePath%>RecordServlet',
                    success: function () {
                    },
                    error: function () {
                    }
                }
            );
        }
        //收藏
        function collectBlog() {
                <%
                    article.setLikingDegree(article.getLikingDegree()+2);
                %>
                $.ajax(
                    {
                        data: {"addCollectionArticleId": "<%=article.getId()%>"},
                        dataType: 'text',
                        type: 'post',
                        url: '<%=basePath%>AddCollectionServlet',
                        success: function () {
                            alert("收藏成功");
                            window.location = "<%=basePath%>blogPage.jsp";
                        },
                        error: function () {
                            alert("收藏失败");
                        }
                    }
                );
        }
        //取消收藏
        function cancelCollected(){
                <%
                    article.setLikingDegree(article.getLikingDegree()-2);
                %>
            $.ajax(
                {
                    data: {"deleteCollectionArticleId": "<%=article.getId()%>"},
                    dataType: 'text',
                    type: 'post',
                    url: '<%=basePath%>DeleteCollectionServlet',
                    success: function () {
                        alert("取消收藏成功");
                        window.location = "<%=basePath%>blogPage.jsp";
                    },
                    error: function () {
                        alert("取消收藏失败");
                    }
                }
            );

        }
        //编辑博客
        function editBlog(){
            $.ajax(
                {
                    data: {"editArticleId": "<%=article.getId()%>"},
                    dataType: 'text',
                    type: 'post',
                    url: '<%=basePath%>EditBlogServlet',
                    success: function () {
                        window.location = "<%=basePath%>editBlogPage.jsp";
                    },
                    error: function () {
                        alert("编辑博客失败");
                    }
                }
            );
        }
        //删除博客
        function deleteBlog(){
            $.ajax(
                {
                    data: {"deleteArticleId": "<%=article.getId()%>"},
                    dataType: 'text',
                    type: 'post',
                    url: '<%=basePath%>DeleteBlogServlet',
                    success: function () {
                        alert("删除博客成功");
                        window.location = "<%=basePath%>personalHompage.jsp";
                    },
                    error: function () {
                        alert("删除博客失败");
                    }
                }
            );
        }
        //点赞博客
        function likeBlog(){
            <%
                    article.setLikingDegree(article.getLikingDegree()+1);
                    article.setLike(article.getLike()+1);
             %>
            $.ajax(
                {
                    data: {"addLikeArticleId": "<%=article.getId()%>"},
                    dataType: 'text',
                    type: 'post',
                    url: '<%=basePath%>AddLikeServlet',
                    success: function () {
                        alert("博客点赞成功");
                        window.location = "<%=basePath%>blogPage.jsp";
                    },
                    error: function () {
                        alert("博客点赞失败");
                    }
                }
            );
        }
        function show(){
            alert(document.getElementById("commentContent").value);
        }
        //点赞取消
        function  cancelLiked(){
            <%
                article.setLikingDegree(article.getLikingDegree()-1);
                article.setLike(article.getLike()-1);
            %>
            $.ajax(
                {
                    data: {"deleteLikeArticleId": "<%=article.getId()%>"},
                    dataType: 'text',
                    type: 'post',
                    url: '<%=basePath%>DeleteLikeServlet',
                    success: function () {
                        alert("取消点赞成功");
                        window.location = "<%=basePath%>blogPage.jsp";
                    },
                    error: function () {
                        alert("取消点赞失败");
                    }
                }
            );
        }

        <%--//评论博客--%>
        <%--function commentBlog(){--%>
        <%--    let content=document.getElementById("commentContent").value;--%>
        <%--    if(content===""){--%>
        <%--        alert("评论不能为空");--%>
        <%--    }else{--%>
        <%--        //评论提交--%>
        <%--        $.ajax(--%>
        <%--            {--%>
        <%--                data: {"comment":content,"blog":blog,"time":date,"commenter":commenter},--%>
        <%--                dataType: 'text',--%>
        <%--                type: 'post',--%>
        <%--                url: '<%=basePath%>PublishCommentServlet',--%>
        <%--                success: function () {--%>
        <%--                    <%--%>
        <%--                    article.setCommentCount(article.getCommentCount()+1);--%>
        <%--                    %>--%>
        <%--                    document.getElementById("commentContent").value="";--%>
        <%--                    document.getElementById("commentCount").value="<%=article.getCommentCount()%>"--%>
        <%--                    $("#commentList").append("<b>"+commenter+"：</b><span>"+content+"                <span>"+date+"</span></span><br><hr>");--%>
        <%--                    window.location="<%=basePath%>blogPage.jsp";--%>
        <%--                },--%>
        <%--                error: function () {--%>
        <%--                    alert("评论失败");--%>
        <%--                }--%>
        <%--            }--%>
        <%--        );--%>

        <%--    }--%>
        <%--}--%>
    </script>
</body>
</html>
